#include "communication/include/readconfig.h"
namespace perception
{

    ReadConfig::ReadConfig(std::string &cell_config_path, std::string &pre_config_path, std::string &obj_config_path) : cell_config_path_(cell_config_path), pre_config_path_(pre_config_path), obj_config_path_(obj_config_path) {}
    ReadConfig::~ReadConfig() {}
    bool ReadConfig::ReadPreconfig()
    {
        std::ifstream jfile(pre_config_path_);
        nlohmann::json value;
        jfile >> value;
        pre_config_.car_config_.car_xmax = value.at("car_config")["car_xmax"].get<float>();
        pre_config_.car_config_.car_ymax = value.at("car_config")["car_ymax"].get<float>();
        pre_config_.car_config_.car_zmax = value.at("car_config")["car_zmax"].get<float>();
        pre_config_.car_config_.car_xmin = value.at("car_config")["car_xmin"].get<float>();
        pre_config_.car_config_.car_ymin = value.at("car_config")["car_ymin"].get<float>();
        pre_config_.car_config_.car_zmin = value.at("car_config")["car_zmin"].get<float>();
        pre_config_.lidar_config_.xmax = value.at("lidar_config")["xmax"].get<float>();
        pre_config_.lidar_config_.ymax = value.at("lidar_config")["ymax"].get<float>();
        pre_config_.lidar_config_.zmax = value.at("lidar_config")["zmax"].get<float>();
        pre_config_.lidar_config_.xmin = value.at("lidar_config")["xmin"].get<float>();
        pre_config_.lidar_config_.ymin = value.at("lidar_config")["ymin"].get<float>();
        pre_config_.lidar_config_.zmin = value.at("lidar_config")["zmin"].get<float>();
        pre_config_.condition_filter_config_.car_config_ = pre_config_.car_config_;
        pre_config_.condition_filter_config_.lidar_config_ = pre_config_.lidar_config_;
        pre_config_.condition_filter_config_.OrAnd = value.at("condition_filter_config")["OrAnd"].get<bool>();
        pre_config_.radius_filter_config_.radius_search = value.at("radius_filter_config")["radius_search"].get<float>();
        pre_config_.radius_filter_config_.search_num = value.at("radius_filter_config")["search_num"].get<int>();
        pre_config_.voxel_filter_config_.voxel_size = value.at("voxel_filter_config")["voxel_size"].get<float>();
        pre_config_.multi_process_points_config_.one_points_max_num = value.at("multi_process_points_config")["one_points_max_num"].get<int>();
        pre_config_.multi_process_points_config_.one_points_min_num = value.at("multi_process_points_config")["one_points_min_num"].get<int>();
        pre_config_.multi_process_points_config_.max_thread_num = value.at("multi_process_points_config")["max_thread_num"].get<int>();
        pre_config_.average_seg_config_.cell_size_x = value.at("average_seg_config")["cell_size_x"].get<float>();
        pre_config_.average_seg_config_.cell_size_y = value.at("average_seg_config")["cell_size_y"].get<float>();
        pre_config_.average_seg_config_.get_ground_points = value.at("average_seg_config")["get_ground_points"].get<bool>();
        pre_config_.average_seg_config_.points_num = value.at("average_seg_config")["points_num"].get<int>();
        pre_config_.average_seg_config_.threshold_h = value.at("average_seg_config")["threshold_h"].get<float>();
        pre_config_.average_seg_config_.xmax = value.at("average_seg_config")["xmax"].get<float>();
        pre_config_.average_seg_config_.xmin = value.at("average_seg_config")["xmin"].get<float>();
        pre_config_.average_seg_config_.ymax = value.at("average_seg_config")["ymax"].get<float>();
        pre_config_.average_seg_config_.ymin = value.at("average_seg_config")["ymin"].get<float>();
        pre_config_.average_seg_config_.ground_height = value.at("average_seg_config")["ground_height"].get<float>();
        pre_config_.average_seg_config_.grid_gradient_eps = value.at("average_seg_config")["grid_gradient_eps"].get<float>();
        pre_config_.average_seg_config_.search_scope = value.at("average_seg_config")["search_scope"].get<float>();
        pre_config_.method = value.at("method").get<int>();
        pre_config_.master_sys_time_eps = value.at("master_sys_time_eps").get<int>();
        pre_config_.master_slave_time_eps = value.at("master_slave_time_eps").get<int>();
        return true;
    }
    bool ReadConfig::ReadCellconfig()
    {
        std::cout << "cell_config_path_:" << cell_config_path_ << std::endl;
        std::ifstream jfile(cell_config_path_);
        nlohmann::json value;
        jfile >> value;

        cell_config_.xmax = value.at("xmax").get<float>();
        cell_config_.xmin = value.at("xmin").get<float>();
        cell_config_.ymax = value.at("ymax").get<float>();
        cell_config_.ymin = value.at("ymin").get<float>();
        cell_config_.zmax = value.at("zmax").get<float>();
        cell_config_.zmin = value.at("zmin").get<float>();
        cell_config_.cell_size_x = value.at("cell_size_x").get<float>();
        cell_config_.cell_size_y = value.at("cell_size_y").get<float>();
        cell_config_.method = value.at("method").get<int>();                     // 1 为老方法 2 为引用lidar_obj的预处理 所有点云一起 3 为引用lidar_obj的预处理 单个点云进行
        cell_config_.min_h = value.at("min_h").get<float>();                     // 最小的障碍物高度
        cell_config_.confidence_ = value.at("confidence_").get<float>();         // cell_config_idence下降速度
        cell_config_.confidence = value.at("confidence").get<float>();           // cell_config_idence下降速度
        cell_config_.density = value.at("density").get<float>();                 // cell_config_idence下降速度
        cell_config_.intercept = value.at("intercept").get<float>();             // cell_config_idence下降速度
        cell_config_.min_point_num_10 = value.at("min_point_num_10").get<int>(); // 十米的点数
        cell_config_.min_point_num_20 = value.at("min_point_num_20").get<int>(); // 20m的点数
        cell_config_.min_point_num_30 = value.at("min_point_num_30").get<int>(); // 30m的点数
        cell_config_.density_10 = value.at("density_10").get<float>();           // density = points_num / 体积
        cell_config_.density_20 = value.at("density_20").get<float>();
        cell_config_.density_30 = value.at("density_30").get<float>();
        cell_config_.dist10_th = value.at("dist10_th").get<float>(); // density = points_num / 体积
        cell_config_.dist20_th = value.at("dist20_th").get<float>();
        cell_config_.dist30_th = value.at("dist30_th").get<float>();
        cell_config_.obj_height = value.at("obj_height").get<float>();
        cell_config_.dist10 = value.at("dist10").get<int>();
        cell_config_.dist20 = value.at("dist20").get<int>();
        cell_config_.dist30 = value.at("dist30").get<int>();
        cell_config_.dbscan_config_.z_eps = value.at("dbscan_config")["z_eps"].get<float>();
        cell_config_.dbscan_config_.min_points_num = value.at("dbscan_config")["min_points_num"].get<int>();
        cell_config_.dbscan_config_.search_scope = value.at("dbscan_config")["search_scope"].get<int>();

        cell_config_.track_config_.max_confidence = value.at("track_config")["max_confidence"].get<float>();
        cell_config_.track_config_.dconfidence = value.at("track_config")["dconfidence"].get<float>();
        cell_config_.track_config_.euclidean_distance = value.at("track_config")["euclidean_distance"].get<float>();
        cell_config_.track_config_.now_v_ratio = value.at("track_config")["now_v_ratio"].get<float>();
        cell_config_.track_config_.max_cluster_size = value.at("track_config")["max_cluster_size"].get<int>();
        cell_config_.track_config_.min_cluster_size = value.at("track_config")["min_cluster_size"].get<int>();
        cell_config_.track_config_.time_eps = value.at("track_config")["time_eps"].get<int>();
        return true;
    }
    bool ReadConfig::ReadObjconfig()
    {
        std::cout << "obj_config_path_:" << obj_config_path_ << std::endl;
        std::ifstream jfile(obj_config_path_);
        nlohmann::json value;
        jfile >> value;

        obj_config_.cuv_angle = value.at("cuv_angle").get<float>();
        obj_config_.eps_angle = value.at("eps_angle").get<float>();
        obj_config_.in_max_cluster_distance = value.at("in_max_cluster_distance").get<float>();
        obj_config_.k_search_num = value.at("k_search_num").get<int>();                   // 1 为老方法 2 为引用lidar_obj的预处理 所有点云一起 3 为引用lidar_obj的预处理 单个点云进行
        obj_config_.tolerance = value.at("tolerance").get<float>();                       // 最小的障碍物高度
        obj_config_.dbscan_eps = value.at("dbscan_eps").get<float>();                     // obj_config_idence下降速度
        obj_config_.dbsacn_min_points_num = value.at("dbsacn_min_points_num").get<int>(); // 十米的点数
        obj_config_.method = value.at("method").get<int>();
        obj_config_.min_cluster_size = value.at("min_cluster_size").get<int>();

        return true;
    }

    //
} // perception